#include "cpp-httplib/httplib.h"
#include "Searcher.hpp"

const std::string input = "data/raw_html/raw.txt";
const std::string root_path = "./wwwroot";

void Usage(const char* proc)
{
    std::cout<<"Usage: "<<proc<<" port"<<std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(1);
    }
    int port=std::stoi(argv[1]);

    ns_searcher::Searcher search;
    search.InitSearcher(input);

    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &rsp){
            if(!req.has_param("word")){
                rsp.set_content("必须要有搜索关键字!", "text/plain; charset=utf-8"); //第二个参数是Content-Type，详细参考对照表
                return;
            }
            std::string word = req.get_param_value("word");
            LOG(LogLevel::INFO)<<"用户搜索的: " + word;

            std::string json_string;
            search.Search(word, &json_string);
            rsp.set_content(json_string, "application/json");
            });

    LOG(LogLevel::INFO)<<"服务器启动成功...";
    svr.listen("0.0.0.0", port);
    return 0;
}
